package com.cz.video.auth.controller;

import cn.dev33.satoken.annotation.SaIgnore;
import com.cz.video.auth.service.SysLoginService;
import com.cz.video.common.constant.Constants;
import com.cz.video.common.core.controller.BaseController;
import com.cz.video.common.core.domain.R;
import com.cz.video.common.core.domain.entity.SysUser;
import com.cz.video.common.core.domain.model.EmailLoginBody;
import com.cz.video.common.core.domain.model.LoginBody;
import com.cz.video.common.core.domain.model.LoginUser;
import com.cz.video.common.helper.LoginHelper;
import com.cz.video.system.api.RemoteUserService;
import io.swagger.v3.oas.annotations.Operation;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * 登录验证
 *
 * @author cz
 */
@Validated
@RequiredArgsConstructor
@RestController()
//@RequestMapping("/auth")
@SaIgnore
public class SysLoginController extends BaseController {

    private final SysLoginService loginService;
//    private final ISysMenuService menuService;
    private final RemoteUserService remoteUserService;

    /**
     * 登录方法
     *
     * @param loginBody 登录信息
     * @return 结果
     */

    @PostMapping("/login")
    public R<Map<String, Object>> login(@Validated @RequestBody LoginBody loginBody) {
        Map<String, Object> ajax = new HashMap<>();
        // 生成令牌
        String token = loginService.login(loginBody.getUsername(), loginBody.getPassword(), loginBody.getCode(),
            loginBody.getUuid());
        ajax.put(Constants.TOKEN, token);
        return R.ok(ajax);
    }


    /**
     * 邮件登录
     *
     * @param body 登录信息
     * @return 结果
     */
    @PostMapping("/emailLogin")
    public R<Map<String, Object>> emailLogin(@Validated @RequestBody EmailLoginBody body) {
        Map<String, Object> ajax = new HashMap<>();
        // 生成令牌
        String token = loginService.emailLogin(body.getEmail(), body.getEmailCode());
        ajax.put(Constants.TOKEN, token);
        return R.ok(ajax);
    }



    /**
     * 退出登录
     */
    @SaIgnore
    @PostMapping("/logout")
    public R<Void> logout() {
        loginService.logout();
        return R.ok("退出成功");
    }

    /**
     * 获取用户信息
     *
     * @return 用户信息
     */
    @GetMapping("getInfo")
    public R<Map<String, Object>> getInfo() {
        LoginUser loginUser = LoginHelper.getLoginUser();
        SysUser user = remoteUserService.selectUserById(loginUser.getUserId());
        Map<String, Object> ajax = new HashMap<>();
        ajax.put("user", user);
        ajax.put("roles", loginUser.getRolePermission());
        ajax.put("permissions", loginUser.getMenuPermission());
        return R.ok(ajax);
    }


    @Operation(summary = "身份验证")
    @PostMapping("/identityAuth")
    public R<String> identityAuth(@Validated @RequestBody EmailLoginBody body) {
        String secretKey = loginService.identityAuth(body.getEmail(), body.getEmailCode());
        R<String> r = R.ok();
        r.setData(secretKey);
        return r;
    }

    @Operation(summary = "重置密码")
    @PostMapping("/resetPassword")
    public R<Void> resetPassword(@Validated @RequestBody EmailLoginBody body) {
        return toAjax(loginService.resetPassword(body));
    }

    @GetMapping("/testRemote")
    public R<Void> test(){
        loginService.testRemoteCall();
        return R.ok();
    }

}
